package com.aode.music.view.fragment.mainfragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.aode.music.R;
import com.aode.music.adapter.page.ChatPagerAdapter;
import com.aode.music.databinding.FragmentChatBinding;
import com.google.android.material.tabs.TabLayout;

public class ChatFragment extends Fragment implements TabLayout.OnTabSelectedListener {
    private FragmentChatBinding chatBinding;
    private TabLayout tabLayout;
    private ViewPager viewPager;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        chatBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_chat,container,false);
        initView();
        return chatBinding.getRoot();
    }

    private void initView() {
        tabLayout = chatBinding.tabLayout;
        viewPager = chatBinding.viewPager;
        tabLayout.addTab(tabLayout.newTab().setText("消息"));
        tabLayout.addTab(tabLayout.newTab().setText("搜索"));
        viewPager.setAdapter(new ChatPagerAdapter(getChildFragmentManager(),tabLayout.getTabCount()));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(this);

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

}
